<template>
    <n-drawer :show="active" :width="width" :mask-closable="false" @update:show="(flag:boolean) => (active = flag)">
        <n-drawer-content :native-scrollbar="false" title="标记完成" closable>
            <n-space vertical :size="12">
                <n-alert title="提示" type="warning">您将对以下提现申请标记完成操作，请确认是否已成功转账</n-alert>
                <n-list bordered>
                    <n-list-item v-for="item in items">
                        <n-space>
                            <n-text code>{{ item.id }}</n-text>
                            <n-text>{{ item.apply.mob }}</n-text>
                            <n-text>{{ item.apply.name }}</n-text>
                            <n-text>{{ item.payee.name }}</n-text>
                            <n-text>{{ (item.value / 100).toFixed(2) }}</n-text>
                        </n-space>
                    </n-list-item>
                </n-list>

                <n-divider />
                <n-form :model="model" :rules="rules" ref="formRef">
                    <n-form-item label="原因" path="memo">
                        <n-input placeholder="请填写说明标记成功原因，例：经线下转账成功" v-model:value="model.memo" maxlength="128" show-count clearable />
                    </n-form-item>
                </n-form>
            </n-space>
            <template #footer>
                <n-space justify="end">
                    <n-button type="tertiary" @click="() => (active = false)">
                        <template #icon>
                            <n-icon :component="CloseRound" />
                        </template>
                        关闭
                    </n-button>
                    <n-button type="warning" :loading="loading" @click="execSave">
                        <template #icon>
                            <n-icon :component="CheckRound" />
                        </template>
                        标记完成
                    </n-button>
                </n-space>
            </template>
        </n-drawer-content>
    </n-drawer>
</template>

<script lang="ts" setup>
import { computed, PropType, ref, onBeforeMount } from 'vue'
import { CloseRound, CheckRound } from '@vicons/material'

import { Http } from '@/libs'
import { useStore } from '@/store'
const store = useStore()
const width = computed(() => Math.max(store.state.browser.innerWidth * 0.4, 640))

const { $notification: notification } = window

const loading = ref(false)

const prop = defineProps({
    show: {
        type: Boolean,
        required: true,
    },
    items: {
        type: Array as PropType<any[]>,
        required: true,
    },
})

const emit = defineEmits(['update:show', 'refresh'])

const active = computed({
    get: () => prop.show,
    set: val => emit('update:show', val),
})

interface ModelType {
    ids: string[]
    memo: string
}

const formRef = ref<any>(null)

const model = ref<ModelType>({
    ids: [],
    memo: '',
})

const rules = {
    memo: {
        required: true,
        trigger: ['blur', 'input'],
        message: '请填写标记原因',
    },
}

const execReset = () => {
    model.value = {
        ids: prop.items.map((v: any) => v.id),
        memo: '',
    }
}

const execSave = (e: Event) => {
    e.preventDefault()
    formRef.value.validate((errors: any[]) => {
        if (!errors) {
            loading.value = true
            Http.post('/ops/fin/withdraw/finish', model.value).then(resp => {
                if (resp.stat.err > 0) {
                    loading.value = false
                    return
                }
                emit('refresh', {})
                notification.success({
                    duration: 1500,
                    title: '操作成功',
                    content: '您已成功标记提现为完成状态',
                })
                setTimeout(() => {
                    loading.value = false
                    emit('update:show', false)
                }, 200)
            })
        } else {
            let msgs = errors.map(err => err[0].message)
            notification.error({
                duration: 1500,
                title: '请填写完整内容',
                description: '当前表单存在以下问题，请处理后提交',
                content: msgs.join('\n'),
            })
        }
    })
}

onBeforeMount(() => {
    execReset()
})
</script>
